﻿package mortal.game.view.forging.view
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.events.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.common.swfPlayer.*;
    import mortal.common.swfPlayer.data.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.common.util.*;
    import mortal.game.view.forging.data.*;
    import mortal.game.view.palyer.*;
    import mortal.mvc.core.*;

    public class EquipColorAdvancePanel extends ForgingPanelBase
    {
        private var _currColorInfo:EquipColorInfoView;
        private var _reviewColorInfo:EquipColorInfoView;
        private var _consumes:Array;
        private var _bestReviewEquip:PlayerEquipItem;
        private var _advanceBtn:GButton;
        private var _upFee:GTextFiled;
        private var _consumeBg:GImageBitmap;
        private var _consumeTxt:GTextFiled;
        private var _isPropEnough:Boolean;
        private var _isMoneyEnough:Boolean;
        private var _putInEffect1:SWFPlayer;
        private var _putInEffect2:SWFPlayer;
        private var _isPlaying1:Boolean;
        private var _isPlaying2:Boolean;
        private var _advanceEffect:SWFPlayer;
        private var _isPlaying3:Boolean;

        public function EquipColorAdvancePanel(param1:Window = null)
        {
            this._consumes = new Array();
            super(param1);
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.createChildren();
            return;
        }// end function

        override protected function createChildren() : void
        {
            var _loc_1:int = 0;
            var _loc_2:BaseItem = null;
            super.createChildren();
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.EquipColorPart, 52 - 8, 62, this));
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.Forging_EquipColorShading, 129, 353, this));
            _currOperEquip = UICompomentPool.getUICompoment(PlayerEquipItem);
            _currOperEquip.isShowArroundEffect = false;
            this._currColorInfo = UICompomentPool.getUICompoment(EquipColorInfoView);
            this._currColorInfo.x = 28;
            this._currColorInfo.y = 34;
            this.addChild(this._currColorInfo);
            this._currColorInfo.isCurrColor = true;
            this._reviewColorInfo = UICompomentPool.getUICompoment(EquipColorInfoView);
            this._reviewColorInfo.x = 279;
            this._reviewColorInfo.y = 34;
            this.addChild(this._reviewColorInfo);
            this._reviewColorInfo.isCurrColor = false;
            this._bestReviewEquip = UICompomentPool.getUICompoment(PlayerEquipItem);
            this._bestReviewEquip.setItemStyle(ItemStyleConst.Small, ImagesConst.EquipItemBg, 5, 5);
            this._bestReviewEquip.x = 12;
            this._bestReviewEquip.y = 10;
            this.addChild(this._bestReviewEquip);
            this._bestReviewEquip.isShowArroundEffect = false;
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.Forging_StrengBestPreview, 6, 55, this));
            this._consumeBg = UIFactory.gImageBitmap(ImagesConst.Forging_ColorConsumeBg1, 189, 100 - 9, this);
            this._consumeTxt = UIFactory.gTextField(Language.getString(41099), 229 - 13, 88, 64, 20, this, GlobalStyle.textFormatLv);
            _loc_1 = 0;
            while (_loc_1 < 2)
            {
                
                _loc_2 = UICompomentPool.getUICompoment(BaseItem);
                _loc_2.setItemStyle(ItemStyleConst.Small, "", 2, 2);
                _loc_2.x = 226;
                _loc_2.y = 76 + 81 * _loc_1;
                this.addChild(_loc_2);
                this._consumes.push(_loc_2);
                this.pushUIToDisposeVec(_loc_2);
                _loc_1++;
            }
            this._advanceBtn = UIFactory.gButton(Language.getString(41100), 208, 366, 82, 28, this, "BrownBtn");
            this._advanceBtn.toolTipData = Language.getString(41101);
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(41102), 325 - 142, 410, 88, 20, this, GlobalStyle.textFormatHuang));
            this._upFee = UIFactory.gTextField("", 411 - 142, 410, 50, 20, this, GlobalStyle.textFormatBai);
            this.pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.Jinbi_bind, 442 - 142 + 9, 409, this));
            this._putInEffect1 = UICompomentPool.getUICompoment(SWFPlayer);
            this._putInEffect2 = UICompomentPool.getUICompoment(SWFPlayer);
            this._advanceEffect = UICompomentPool.getUICompoment(SWFPlayer);
            this._advanceBtn.configEventListener(MouseEvent.CLICK, this.onAdvanceHandler);
            return;
        }// end function

        override public function updateUI() : void
        {
            super.updateUI();
            return;
        }// end function

        override public function addForgingEquip(param1:PlayerEquipItem) : void
        {
            var _loc_2:int = 0;
            var _loc_3:ItemData = null;
            if (param1 && param1.itemData)
            {
                super.addForgingEquip(param1);
                this._currColorInfo.updateColorInfo(param1.itemData);
                _loc_2 = _currOperEquip.itemData.itemInfo.color;
                _loc_3 = this.getColorData((_loc_2 + 1));
                this._reviewColorInfo.updateColorInfo(_loc_3);
                this.updateCombat();
                this.updateBestReview();
                this.updateConsumes();
                this.stopAllEffect();
                this.playPutInEffect();
            }
            return;
        }// end function

        override public function updateForgingInfo() : void
        {
            this._currColorInfo.updateColorInfo(_currOperEquip.itemData);
            var _loc_1:* = _currOperEquip.itemData.itemInfo.color;
            var _loc_2:* = _currOperEquip.itemData.extInfo.combat;
            var _loc_3:* = this.getColorData((_loc_1 + 1));
            this._reviewColorInfo.updateColorInfo(_loc_3);
            this._currColorInfo.updateEquipCombat(_loc_2);
            this.updateConsumes();
            if (_loc_3)
            {
                _loc_3.extInfo.combat = EquipmentUtil.getEquipCombat(_loc_3);
                this._reviewColorInfo.updateEquipCombat(_loc_3.extInfo.combat);
            }
            this.stopAllEffect();
            this.playAdvanceEffect();
            return;
        }// end function

        private function onAdvanceHandler(event:MouseEvent) : void
        {
            if (!_currOperEquip.itemData)
            {
                MsgManager.showRollTipsMsg(Language.getString(41103), MsgRollTipsType.msgRollTips1);
                return;
            }
            if (!this._isPropEnough)
            {
                MsgManager.showRollTipsMsg(Language.getString(41104), MsgRollTipsType.msgRollTips3);
                return;
            }
            if (!this._isMoneyEnough)
            {
                MsgManager.showRollTipsMsg(Language.getString(41105), MsgRollTipsType.msgRollTips3);
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShowBindCoinPackWin));
                return;
            }
            var _loc_2:* = _currOperEquip.itemData.uid;
            Dispatcher.dispatchEvent(new DataEvent(EventName.EquipColorAdvance, _loc_2));
            return;
        }// end function

        private function getColorData(param1:int) : ItemData
        {
            var _loc_7:ItemData = null;
            var _loc_8:Dictionary = null;
            var _loc_9:ItemEquipInfo = null;
            var _loc_2:* = _currOperEquip.itemData.itemInfo.type;
            var _loc_3:* = _currOperEquip.itemData.itemInfo.career;
            var _loc_4:* = (_currOperEquip.itemData.itemInfo as ItemEquipInfo).suitGroup;
            var _loc_5:* = _currOperEquip.itemData.itemInfo.level;
            var _loc_6:* = EquipAdvanceConfig.instance.getTargetSuitGroup(_loc_4);
            if (_loc_6 != 0)
            {
                _loc_8 = ItemConfig.instance.getAllEquip();
                for each (_loc_9 in _loc_8)
                {
                    
                    if (_loc_9.suitGroup == _loc_6 && _loc_9.type == _loc_2 && _loc_9.career == _loc_3 && _loc_9.level == _loc_5)
                    {
                        _loc_7 = new ItemData(_loc_9.code, true);
                        _loc_7.extInfo = _currOperEquip.itemData.extInfo.clone();
                        _loc_7.extInfo.hole_num = EquipmentUtil.getGemHoleNum(_loc_7);
                        break;
                    }
                }
            }
            return _loc_7;
        }// end function

        override public function clearUI() : void
        {
            this._currColorInfo.clearUI();
            this._reviewColorInfo.clearUI();
            this.clearConsumes();
            this._bestReviewEquip.itemData = null;
            this._bestReviewEquip.updateStrengLevel();
            this._advanceBtn.filters = [];
            this._advanceBtn.mouseEnabled = true;
            return;
        }// end function

        private function clearConsumes() : void
        {
            var _loc_1:int = 0;
            while (_loc_1 < this._consumes.length)
            {
                
                this._consumes[_loc_1].itemData = null;
                _loc_1++;
            }
            this._upFee.text = "";
            this._consumeBg.imgUrl = ImagesConst.Forging_ColorConsumeBg1;
            this._consumeBg.x = 189;
            this._consumeBg.y = 91;
            this._consumeTxt.y = 88;
            this._isPropEnough = true;
            this._isMoneyEnough = true;
            return;
        }// end function

        private function updateConsumes() : void
        {
            var _loc_5:String = null;
            var _loc_6:int = 0;
            var _loc_7:Array = null;
            var _loc_8:int = 0;
            var _loc_9:Array = null;
            var _loc_10:int = 0;
            var _loc_11:int = 0;
            var _loc_12:ItemData = null;
            var _loc_13:BaseItem = null;
            this.clearConsumes();
            if (!this._reviewColorInfo.advancedEquip.itemData)
            {
                return;
            }
            var _loc_1:* = (_currOperEquip.itemData.itemInfo as ItemEquipInfo).suitGroup;
            var _loc_2:* = EquipmentUtil.getCategoryByEquipType(_currOperEquip.itemData.itemInfo.type);
            var _loc_3:* = _currOperEquip.itemData.itemInfo.type == EEquip._EEquipWeapon ? (1) : (0);
            var _loc_4:* = EquipAdvanceConfig.instance.getColorInfo(_loc_1, _currOperEquip.itemData.itemInfo.type);
            if (_loc_4 != null)
            {
                _loc_5 = _loc_4.consumeItem;
                _loc_6 = _loc_4.consumeMoney;
                _loc_7 = _loc_5.split("&");
                _loc_8 = 0;
                while (_loc_8 < _loc_7.length)
                {
                    
                    _loc_9 = _loc_7[_loc_8].split("#");
                    _loc_10 = int(_loc_9[0]);
                    _loc_11 = int(_loc_9[1]);
                    _loc_12 = new ItemData(_loc_10);
                    (this._consumes[_loc_8] as BaseItem).itemData = _loc_12;
                    (this._consumes[_loc_8] as BaseItem).amount = _loc_11;
                    _loc_8++;
                }
                if (_loc_7.length == 1)
                {
                    this._consumeBg.imgUrl = ImagesConst.Forging_ColorConsumeBg1;
                    this._consumeBg.x = 189;
                    this._consumeBg.y = 91;
                    this._consumeTxt.y = 88;
                    this._consumes[0].y = 76 + 40;
                }
                else if (_loc_7.length == 2)
                {
                    this._consumeBg.imgUrl = ImagesConst.Forging_ColorConsumeBg2;
                    this._consumeBg.x = 186;
                    this._consumeBg.y = 70;
                    this._consumeTxt.y = 44;
                    this._consumes[0].y = 76;
                    this._consumes[1].y = 76 + 81;
                }
                this.updateConsumeState();
                this._upFee.text = "" + _loc_6;
                if (_loc_6 > Cache.instance.role.money.coin + Cache.instance.role.money.coinBind)
                {
                    this._upFee.textColor = GlobalStyle.colorHongUint;
                    this._isMoneyEnough = false;
                }
                else
                {
                    this._upFee.textColor = GlobalStyle.colorHuangUint;
                    this._isMoneyEnough = true;
                }
            }
            else
            {
                for each (_loc_13 in this._consumes)
                {
                    
                    _loc_13.itemData = null;
                }
                this._upFee.text = "";
            }
            return;
        }// end function

        override public function updateConsumeState() : void
        {
            var _loc_2:ItemData = null;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_1:int = 0;
            while (_loc_1 < this._consumes.length)
            {
                
                _loc_2 = (this._consumes[_loc_1] as BaseItem).itemData;
                if (_loc_2)
                {
                    _loc_3 = _loc_2.itemInfo.type;
                    _loc_4 = (this._consumes[_loc_1] as BaseItem).amount;
                    _loc_5 = Cache.instance.pack.backPackCache.getItemCountByItemData(_loc_2);
                    if (_loc_4 <= _loc_5)
                    {
                        (this._consumes[_loc_1] as BaseItem).isGray = false;
                        this._isPropEnough = true;
                    }
                    else
                    {
                        (this._consumes[_loc_1] as BaseItem).isGray = true;
                        this._isPropEnough = false;
                    }
                }
                _loc_1++;
            }
            return;
        }// end function

        override public function updateMoneyState() : void
        {
            var _loc_1:int = 0;
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:TEquipAdvance = null;
            var _loc_5:int = 0;
            if (_currOperEquip.itemData)
            {
                _loc_1 = (_currOperEquip.itemData.itemInfo as ItemEquipInfo).suitGroup;
                _loc_2 = EquipmentUtil.getCategoryByEquipType(_currOperEquip.itemData.itemInfo.type);
                _loc_3 = _currOperEquip.itemData.itemInfo.type == EEquip._EEquipWeapon ? (1) : (0);
                _loc_4 = EquipAdvanceConfig.instance.getColorInfo(_loc_1, _currOperEquip.itemData.itemInfo.type);
                if (_loc_4)
                {
                    _loc_5 = _loc_4.consumeMoney;
                    if (_loc_5 > Cache.instance.role.money.coin + Cache.instance.role.money.coinBind)
                    {
                        this._upFee.textColor = GlobalStyle.colorHongUint;
                        this._isMoneyEnough = false;
                    }
                    else
                    {
                        this._upFee.textColor = GlobalStyle.colorHuangUint;
                        this._isMoneyEnough = true;
                    }
                }
            }
            return;
        }// end function

        private function updateBestReview() : void
        {
            var _loc_1:* = this.getColorData(ForgingConst.MaxColorLevel);
            this._bestReviewEquip.itemData = _loc_1;
            if (_loc_1 != null)
            {
                this._bestReviewEquip.itemData.extInfo.combat = EquipmentUtil.getEquipCombat(_loc_1);
            }
            this._bestReviewEquip.updateStrengLevel();
            return;
        }// end function

        private function updateCombat() : void
        {
            this._currColorInfo.updateEquipCombat(EquipmentUtil.getEquipCombat(_currOperEquip.itemData));
            var _loc_1:* = this._reviewColorInfo.advancedEquip.itemData;
            if (_loc_1)
            {
                _loc_1.extInfo.combat = EquipmentUtil.getEquipCombat(_loc_1);
                this._reviewColorInfo.updateEquipCombat(_loc_1.extInfo.combat);
            }
            return;
        }// end function

        private function updateBtnState() : void
        {
            if (_currOperEquip.itemData && this._reviewColorInfo.advancedEquip.itemData)
            {
                this._advanceBtn.filters = [];
                this._advanceBtn.mouseEnabled = true;
            }
            else
            {
                this._advanceBtn.filters = [FilterConst.colorFilter2];
                this._advanceBtn.mouseEnabled = false;
            }
            return;
        }// end function

        private function playPutInEffect() : void
        {
            var _loc_2:String = null;
            var _loc_3:String = null;
            var _loc_1:* = _currOperEquip.itemData.itemInfo.color;
            switch(_loc_1)
            {
                case ForgingConst.EquipColor_Zi:
                {
                    _loc_2 = "PutInEquipEffect_Zi.swf";
                    _loc_3 = "PutInEquipEffect_Cheng.swf";
                    break;
                }
                case ForgingConst.EquipColor_Cheng:
                {
                    _loc_2 = "PutInEquipEffect_Cheng.swf";
                    _loc_3 = "PutInEquipEffect_Hong.swf";
                    break;
                }
                case ForgingConst.EquipColor_Hong:
                {
                    _loc_2 = "PutInEquipEffect_Hong.swf";
                    _loc_3 = "";
                    break;
                }
                default:
                {
                    break;
                }
            }
            if (!this._reviewColorInfo.advancedEquip.itemData)
            {
                _loc_3 = "";
            }
            this._putInEffect1.move(121, 133);
            this._putInEffect1.load(_loc_2, ModelType.NormalSwf, null);
            this.addChild(this._putInEffect1);
            this._isPlaying1 = true;
            if (_loc_3 != "")
            {
                this._putInEffect2.move((370 - 1), 136 - 4);
                this._putInEffect2.load(_loc_3, ModelType.NormalSwf, null);
                this.addChild(this._putInEffect2);
                this._isPlaying2 = true;
            }
            return;
        }// end function

        private function firstEffectEndHandler(param1:SWFPlayer) : void
        {
            this._putInEffect1.framesPlayerCompleteHandler = null;
            this._putInEffect1.stop();
            DisplayUtil.removeMe(this._putInEffect1);
            this._isPlaying1 = false;
            return;
        }// end function

        private function secondEffectEndHandler(param1:SWFPlayer) : void
        {
            this._putInEffect2.framesPlayerCompleteHandler = null;
            this._putInEffect2.stop();
            DisplayUtil.removeMe(this._putInEffect2);
            this._isPlaying2 = false;
            return;
        }// end function

        private function playAdvanceEffect() : void
        {
            var _loc_2:String = null;
            this.stopAllEffect();
            var _loc_1:* = _currOperEquip.itemData.itemInfo.color;
            switch(_loc_1)
            {
                case ForgingConst.EquipColor_Zi:
                {
                    _loc_2 = "AdvanceEffect_Back_Zi.swf";
                    break;
                }
                case ForgingConst.EquipColor_Cheng:
                {
                    _loc_2 = "AdvanceEffect_Back_Cheng.swf";
                    break;
                }
                case ForgingConst.EquipColor_Hong:
                {
                    _loc_2 = "AdvanceEffect_Back_Hong.swf";
                    break;
                }
                default:
                {
                    break;
                }
            }
            this._advanceEffect.move(370, 136);
            this._advanceEffect.load(_loc_2, ModelType.NormalSwf, null);
            this._advanceEffect.framesPlayerCompleteHandler = this.backEffectEnd;
            this.addChild(this._advanceEffect);
            this._isPlaying3 = true;
            return;
        }// end function

        private function backEffectEnd(param1:SWFPlayer) : void
        {
            this._advanceEffect.framesPlayerCompleteHandler = null;
            this._advanceEffect.stop();
            DisplayUtil.removeMe(this._advanceEffect);
            this._isPlaying3 = false;
            this.playPutInEffect();
            return;
        }// end function

        public function stopAllEffect() : void
        {
            if (this._isPlaying1)
            {
                this._putInEffect1.stop();
                DisplayUtil.removeMe(this._putInEffect1);
                this._isPlaying1 = false;
            }
            if (this._isPlaying2)
            {
                this._putInEffect2.stop();
                DisplayUtil.removeMe(this._putInEffect2);
                this._isPlaying2 = false;
            }
            if (this._isPlaying3)
            {
                this._advanceEffect.stop();
                DisplayUtil.removeMe(this._advanceEffect);
                this._isPlaying3 = false;
            }
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            if (_isCreateChildren)
            {
                _isCreateChildren = false;
                this._currColorInfo.dispose(param1);
                this._reviewColorInfo.dispose(param1);
                this._bestReviewEquip.dispose(param1);
                this._advanceBtn.dispose(param1);
                this._upFee.dispose(param1);
                this._putInEffect1.dispose(param1);
                this._putInEffect2.dispose(param1);
                this._advanceEffect.dispose(param1);
                this._consumeBg.dispose(param1);
                this._consumeTxt.dispose(param1);
                this._currColorInfo = null;
                this._reviewColorInfo = null;
                this._consumes.length = 0;
                this._bestReviewEquip = null;
                this._advanceBtn = null;
                this._upFee = null;
                this._putInEffect1 = null;
                this._putInEffect2 = null;
                this._advanceEffect = null;
                this._consumeBg = null;
                this._consumeTxt = null;
            }
            this._isPlaying1 = false;
            this._isPlaying2 = false;
            this._isPlaying3 = false;
            this._isPropEnough = true;
            this._isMoneyEnough = true;
            return;
        }// end function

    }
}
